<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  if(isset($_POST['editbutton']) && isset($_POST['logSheetId']) && $_POST['logSheetId'] > 0)
  {
    $logsheetDate     = $_POST['logDateYear']."-".$_POST['logDateMonth']."-".$_POST['logDateDay'];
   	$startTime        = date('y-m-d')." ".$_POST['startTimeHour'].":".$_POST['startTimeMinute'].":00";
   	$soakingStartTime = date('y-m-d')." ".$_POST['endTimeHour'].":".$_POST['endTimeMinute'].":00";
   	$endTime          = date('y-m-d')." ".$_POST['soakStartTimeHour'].":".$_POST['soakStartTimeMinute'].":00";

    $updateLogSheet = "UPDATE logsheet 
                          SET logsheetNo = ".$_POST['logsheetNo'].",
                              logsheetDate = '".$logsheetDate."',
                              startTime = '".$startTime."',
                              startTemperature = '".$_POST['startTemperature']."',
                              soakingStartTime = '".$soakingStartTime."',
                              endTime = '".$endTime."',
                              endTemperature = '".$_POST['endTemperature']."',
                              qMId = ".$_POST['quenchingMedia'].",
                              quenchingTime = ".$_POST['quenchingTime'].",
                              tempBforQue = '".$_POST['tempBforQue']."',
                              tempAfterQue = '".$_POST['tempAfterQue']."'
                        WHERE logSheetId = ".$_POST['logSheetId'];
    $updateLogSheetRes = mysql_query($updateLogSheet);
    if($updateLogSheetRes)
      header('Location:furnaceReport.php');
  }
  $incommingArr = array();
  $processArr = array();
  
  $selectincomingProcess = "SELECT planning.incomingId,processId1,processId2,processId3,pro1.processName AS proName1,
                                   pro2.processName AS proName2,pro3.processName AS proName3,
                                   temp1,soaking1,furnace1.furnaceName AS fName1,
                                   temp2,soaking2,furnace2.furnaceName AS fName2,
                                   temp3,soaking3,furnace3.furnaceName AS fName3,okNotOk1,okNotOk2,okNotOk3
                              FROM planning
                              JOIN process AS pro1 ON planning.processId1 = pro1.processId
                              LEFT JOIN process AS pro2 ON planning.processId2 = pro2.processId
                              LEFT JOIN process AS pro3 ON planning.processId3 = pro3.processId
                              LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                              LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                              LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
                             WHERE planningId = ".$_GET['planningId'];
  $selectincomingProcessRes = mysql_query($selectincomingProcess);
  if($processRow = mysql_fetch_array($selectincomingProcessRes))
  {
    $incommingArr['logSheetPlanningId'] = $_GET['planningId'];
    $incommingArr['logSheetIncomingId'] = $processRow['incomingId'];
    
    $incommingArr['temp1']        = $processRow['temp1'];
    $incommingArr['soaking1']     = $processRow['soaking1'];
    $incommingArr['furnace1']     = $processRow['fName1'];
    
    $incommingArr['temp2']        = $processRow['temp2'];
    $incommingArr['soaking2']     = $processRow['soaking2'];
    $incommingArr['furnace2']     = $processRow['fName2'];
    
    $incommingArr['temp3']        = $processRow['temp3'];
    $incommingArr['soaking3']     = $processRow['soaking3'];
    $incommingArr['furnace3']     = $processRow['fName3'];
    
    $incommingArr['processName1'] = $processRow['proName1'];  
    $incommingArr['processName2'] = $processRow['proName2'];
    $incommingArr['processName3'] = $processRow['proName3'];
    
    $selectEmptySheet = "SELECT * FROM planning WHERE planningId = ".$_GET['planningId'];
    $selRes = mysql_query($selectEmptySheet);
    $j = 0;
    if($planRow = mysql_fetch_array($selRes))
    {
    	if($planRow['processId1'] > 0 && $planRow['temp1'] > 0 && $planRow['logSheet1'] == 0)
    	{
    		$j++;
    		$incommingArr['processId'][$j] = $planRow['processId1'];
    		$incommingArr['nameOfProcess'][$j] = $processRow['proName1'];
    		$incommingArr['processNo'][$j] = 1;
    	}
    	if($planRow['processId2'] > 0 && $planRow['temp2'] > 0 && $planRow['logSheet2'] == 0)
    	{
    		$j++;
    		$incommingArr['processId'][$j] = $planRow['processId2'];
    		$incommingArr['nameOfProcess'][$j] = $processRow['proName2'];
    		$incommingArr['processNo'][$j] = 2;
    	}
    	if($planRow['processId3'] > 0 && $planRow['temp3'] > 0 && $planRow['logSheet3'] == 0)
    	{
    		$j++;
    		$incommingArr['processId'][$j] = $planRow['processId3'];
    		$incommingArr['nameOfProcess'][$j] = $processRow['proName3'];
    		$incommingArr['processNo'][$j] = 3;
    	}
    	
    	/////////////////////////////////
    	for($processLoop = 1; $processLoop <= 3; $processLoop++)
    	{
    	  if(isset($planRow['logSheet'.$processLoop]) && $planRow['logSheet'.$processLoop] > 0)
    	  {
  	      $selEditLogData = "SELECT logSheetId,logsheet.incomingId,logsheet.processId,logsheetNo,
  	                                DATE_FORMAT(logsheetDate,'%d-%m-%Y') AS logsheetDate,
  	                                DATE_FORMAT(startTime,'%H-%i')AS startTime,startTime AS showStartTime,
  	                                startTemperature,
  	                                DATE_FORMAT(soakingStartTime,'%H-%i')AS soakingStartTime,soakingStartTime AS showSoakingStartTime,
  	                                DATE_FORMAT(endTime,'%H-%i')AS endTime,endTime AS showEndTime,
  	                                endTemperature,logsheet.qMId,
                                    qMName,quenchingTime,tempBforQue,tempAfterQue,processName,logsheet.status
                               FROM logsheet
                               JOIN process ON process.processId = logsheet.processId
                               JOIN quenchingmedia ON logsheet.qMId = quenchingmedia.qMId
                              WHERE logsheetId = ".$planRow['logSheet'.$processLoop];
          $selEditLogDataRes = mysql_query($selEditLogData);
          if($processRow = mysql_fetch_array($selEditLogDataRes))
          {
          	if($processRow['logSheetId'] == $_GET['logSheetId'])
          	{
          		$processArr[$processLoop-1]['toBeEditRecord'] = 1;
          	}
          	else
          	{
          		$processArr[$processLoop-1]['toBeEditRecord'] = 0;
          	}
            $processArr[$processLoop-1]['logSheetId']         = $processRow['logSheetId'];  
            $processArr[$processLoop-1]['processNo']          = $processRow['processId'];
            $processArr[$processLoop-1]['processName']        = $processRow['processName'];
            $processArr[$processLoop-1]['logsheetNo']         = $processRow['logsheetNo'];
            $processArr[$processLoop-1]['logsheetDate']       = $processRow['logsheetDate'];
            $processArr[$processLoop-1]['startTime']          = $processRow['startTime'];
            $processArr[$processLoop-1]['showStartTime']      = $processRow['showStartTime'];
            $processArr[$processLoop-1]['startTemperature']   = $processRow['startTemperature'];
            $processArr[$processLoop-1]['soakingStartTime']   = $processRow['soakingStartTime'];
            $processArr[$processLoop-1]['showSoakingStartTime'] = $processRow['showSoakingStartTime'];
            $processArr[$processLoop-1]['endTime']            = $processRow['endTime'];
            $processArr[$processLoop-1]['showEndTime']        = $processRow['showEndTime'];
            $processArr[$processLoop-1]['endTemperature']     = $processRow['endTemperature'];
            $processArr[$processLoop-1]['qMId']               = $processRow['qMId'];
            $processArr[$processLoop-1]['quenchingMedia']     = $processRow['qMName'];
            $processArr[$processLoop-1]['quenchingTime']      = $processRow['quenchingTime'];
            $processArr[$processLoop-1]['tempBforQue']        = $processRow['tempBforQue'];
            $processArr[$processLoop-1]['tempAfterQue']       = $processRow['tempAfterQue'];
            $processArr[$processLoop-1]['status']             = $processRow['status'];
            $processArr[$processLoop-1]['incomingId']         = $processRow['incomingId'];
          }
        }
      }
    	/////////////////////////////////
    }
  }
  //-----------------Quenching Media-----------------------------------
  $q = 0;                                                                     
  $select = "SELECT qMId,qMName                                    
               FROM quenchingmedia";                                                   
  $selectResult = mysql_query($select);                                       
  while($row = mysql_fetch_array($selectResult))                              
  {                                                                           
    $qmArray['qMId'][$q]   = $row['qMId'];                    
    $qmArray['qMName'][$q] = $row['qMName'];                      
    $q++;                                                                     
  }                                                                           
  include("./bottom.php");
  $smarty->assign("j",$j);
  $smarty->assign("incommingArr",$incommingArr);
  $smarty->assign("processArr",$processArr);
  $smarty->assign("qmArray",$qmArray);  
  $smarty->display("logsheetEdit.tpl");
}
?>